package com.sdk.bigdata.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.sdk.bigdata.exceptions.DataException;

import java.util.HashMap;
import java.util.Map;

import static com.sdk.bigdata.constant.Constants.TYPE_BUNDLE_DELETE;

// 礼包
public class Bundle extends BaseModel {

    @JSONField(name = "bundle_id")
    private String bundleId;

    private int[][] items;
    private String name;
    private Double price;

    public Bundle(){}

    public Bundle(String type, String bundleId, String name,  Double price, int[][] items, Map<String, Object> properties) {
        super(type, properties);
        this.bundleId = bundleId;
        this.items = items;
        this.name = name;
        this.price = price;
    }

    public String getBundleId() {
        return bundleId;
    }

    public void setBundleId(String bundleId) {
        this.bundleId = bundleId;
    }

    public int[][] getItems() {
        return items;
    }

    public void setItems(int[][] items) {
        this.items = items;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public void validAttribute() {
        if (null == this.getType() || (!this.getType().equals("bundle_set") && !this.getType().equals("bundle_delete"))) {
            throw new DataException("property [Type] is not bundle_set or bundle_delete");
        }
        this.setTime(System.currentTimeMillis());

        // check uid
        if (null == this.bundleId || this.bundleId.equals("")) {
            throw new DataException("property [ItemId] can't be empty");
        }
        if (this.bundleId.length() > 255) {
            throw new DataException("the max length of property [ItemId] is 255");
        }

        if ((null == this.getItems() || this.getItems().length <= 0) && !this.getType().equals(TYPE_BUNDLE_DELETE)) {
            throw new DataException("property [Items] size should >= 1");
        }

        if (null == this.getProperties()) {
            this.setProperties(new HashMap<>());
        }
    }
}
